﻿
var _Areas_maps_InfoWindow;
var _directionsDisplay;
var _geocoder_address ;
var _directionsService;
var _mapsMain;

function initialize_maps() {

    _Areas_maps_InfoWindow = new google.maps.InfoWindow();
    _directionsDisplay = new google.maps.DirectionsRenderer();
    _geocoder_address = new google.maps.Geocoder();
    _directionsService = new google.maps.DirectionsService();

    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: new google.maps.LatLng(10.0322946, 105.7793358),
        zoom: 18,
        disableDefaultUI: true
        //mapTypeControl: false,
        //mapTypeControlOptions: {
        //    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        //}
    };

    _mapsMain = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

    var marker_load_first = new google.maps.Marker({
        position: new google.maps.LatLng(10.0322946, 105.7793358),
        icon: _nameSubDomain + '/Content/images/vietnam_flag_pin_64.png',
        map: _mapsMain,
        title: "Nình kiều - Cần Thơ"
    });

  

}






function change_view_dirctect(clat, clong, name) {
    
    var map_latLng_load_view = new google.maps.LatLng(clat, clong);
    var marker_latLng_load_view = new google.maps.Marker({
        position: map_latLng_load_view,
        icon: _nameSubDomain + '/Content/images/vietnam_flag_pin_64.png',
        map: _mapsMain,
        title: name
    });
    var zoom = _mapsMain.getZoom();
    var bounds = new google.maps.LatLngBounds();
    bounds.extend(map_latLng_load_view);
    _mapsMain.fitBounds(bounds);
    _mapsMain.setZoom(zoom > 18 ? 18 : zoom);


}

function setMapType(type) {
    switch (type) {
        case "ROADMAP":
            _mapsMain.setMapTypeId(google.maps.MapTypeId.ROADMAP);
            break;
        case "SATELLITE":
            _mapsMain.setMapTypeId(google.maps.MapTypeId.SATELLITE);
            break;
        case "HYBRID":
            _mapsMain.setMapTypeId(google.maps.MapTypeId.HYBRID);
            break;
        case "TERRAIN":
            _mapsMain.setMapTypeId(google.maps.MapTypeId.TERRAIN);
            break;
        default:
            _mapsMain.setMapTypeId(google.maps.MapTypeId.ROADMAP);
    }
}


var _mapsMainKS;

function initialize_maps_khao_sat() {

    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: new google.maps.LatLng(10.0322946, 105.7793358),
        zoom: 16,
        disableDefaultUI: true
        
    };

    _mapsMainKS = new google.maps.Map(document.getElementById("map_canvas_ks"), myOptions);

    var marker_load_first_ks = new google.maps.Marker({
        position: new google.maps.LatLng(10.0322946, 105.7793358),
        icon: _nameSubDomain + '/Content/images/vietnam_flag_pin_64.png',
        map: _mapsMainKS,
        title: "Nình kiều - Cần Thơ"
    });

    LoadVnptSiteOnLoaiSite("S16");

}

function change_view_dirctect_ksat(clat, clong, name) {

    var map_latLng_load_view = new google.maps.LatLng(clat, clong);
    var marker_latLng_load_view = new google.maps.Marker({
        position: map_latLng_load_view,
        icon: _nameSubDomain + '/Content/images/vietnam_flag_pin_64.png',
        map: _mapsMainKS,
        title: name
    });
    var zoom = _mapsMainKS.getZoom();
    var bounds = new google.maps.LatLngBounds();
    bounds.extend(map_latLng_load_view);
    _mapsMainKS.fitBounds(bounds);
    _mapsMainKS.setZoom(zoom > 18 ? 18 : zoom);

}

var marker_vnpt_site_load_view_ks;
var vnpt_site_lat_load_view_ks;
var vnpt_site_lng_load_view_ks;
var latLng_vnpt_site_load_view_ks;
var bounds_vnpt_site_load_view_ks;
var imageUrl_vnpt_site_load_view_ks;
var markers_array_vnpt_site_load_view_ks = [];
var infowindow_vnpt_site_load_view_ks = new google.maps.InfoWindow();

function LoadVnptSiteOnLoaiSite(loaisite) {

    $(".Maps_pageInDex_ks").mask("Dữ liệu đang được xử lý ...");


    setTimeout(function () {

        $.ajax({ url: _nameSubDomain + "/KhaosatLD/ViewSiteOnMaps?loaiSite=" + loaisite, async: false }).done(function (data) {

            while (markers_array_vnpt_site_load_view_ks.length) {
                markers_array_vnpt_site_load_view_ks.pop().setMap(null);
            }

            var vnpt_site = eval(data);

            for (var xy_vnpt_site = 0; xy_vnpt_site < vnpt_site.length; xy_vnpt_site++) {

                if ((vnpt_site[xy_vnpt_site]._entity.Y != "" || vnpt_site[xy_vnpt_site]._entity.Y != null) && (vnpt_site[xy_vnpt_site]._entity.X != "" || vnpt_site[xy_vnpt_site]._entity.X != null)) {

                    imageUrl_vnpt_site_load_view_ks = domain_url + _nameSubDomain + "/Content/fileUpload/" + vnpt_site[xy_vnpt_site].images;


                    marker_vnpt_site_load_view_ks = new google.maps.MarkerImage(imageUrl_vnpt_site_load_view,
                        new google.maps.Size(48, 48));

                    vnpt_site_lat_load_view_ks = parseFloat(vnpt_site[xy_vnpt_site]._entity.Y);
                    vnpt_site_lng_load_view_ks = parseFloat(vnpt_site[xy_vnpt_site]._entity.X);

                    latLng_vnpt_site_load_view_ks = new google.maps.LatLng(vnpt_site_lat_load_view_ks, vnpt_site_lng_load_view_ks);

                    //bounds_vnpt_site_load_view.extend(latLng_vnpt_site_load_view);


                    marker_vnpt_site_load_view_ks = new MarkerWithLabel({
                        position: latLng_vnpt_site_load_view_ks,
                        map: _mapsMainKS,
                        draggable: false,
                        raiseOnDrag: true,
                        crossOnDrag: true,
                        icon: imageUrl_vnpt_site_load_view_ks,
                        labelContent: vnpt_site[xy_vnpt_site]._entity.TEN,
                        labelAnchor: new google.maps.Point(100, 0),
                        labelClass: "labels_vnpt", // the CSS class for the label
                        labelStyle: { opacity: 0.75 },
                        id: vnpt_site[xy_vnpt_site]._entity.SITEID

                    });


                    //market_vnpt_site_load_view.mycategory = vnpt_site[xy_vnpt_site]["loaisite"];


                    markers_array_vnpt_site_load_view_ks.push(marker_vnpt_site_load_view);


                    

                    google.maps.event.addListener(marker_vnpt_site_load_view_ks, 'dblclick', (function (marker_vnpt_site_load_view_ks, xy_vnpt_site) {
                        return function () {
                            try {
                                infowindow_vnpt_site_load_view_ks.close();
                            }
                            catch (exception) { }
                            finally {


                                var vnpt_site_lat_load_info_dbclick = parseFloat(vnpt_site[xy_vnpt_site]._entity.Y);
                                var vnpt_site_lng_load_info_dbclick = parseFloat(vnpt_site[xy_vnpt_site]._entity.X);
                                var lat_long_view_load_infor_dbclick = new google.maps.LatLng(vnpt_site_lat_load_info_dbclick, vnpt_site_lng_load_info_dbclick);

                                $.ajax({
                                    url: _nameSubDomain + '/KhaosatLD/ViewThueBaoOnVnptSite_KS?vnptSiteid=' + vnpt_site[xy_vnpt_site]._entity.SITEID,
                                    success: function (data) {
                                        infowindow_vnpt_site_load_view_ks.setContent(data);
                                        infowindow_vnpt_site_load_view_ks.open(_mapsMainKS);
                                        infowindow_vnpt_site_load_view_ks.setPosition(lat_long_view_load_infor_dbclick);
                                        google.maps.event.addListener(marker_vnpt_site_load_view_ks, 'domready', (function (marker_vnpt_site_load_view_ks, xy_vnpt_site) {
                                            // whatever you want to do once the DOM is ready
                                            LoadJavascripit_View_TB_On_VNPT_SITE(vnpt_site[xy_vnpt_site]._entity.SITEID);

                                        })(marker_vnpt_site_load_view_ks, xy_vnpt_site));
                                    }
                                });

                            }
                        }
                    })(marker_vnpt_site_load_view_ks, xy_vnpt_site));

                }
            }

            setTimeout(function () { $(".Maps_pageInDex_ks").unmask(); }, 2000);

        });

    }, 100);

}

function LoadJavascripit_View_TB_On_VNPT_SITE(id) {

    $(".Maps_pageInDex_ks").mask("Dữ liệu đang xử lý");

    $("#tbas_view_tb_khao_sat").tabs();

    

    //setTimeout(function () {

    //    $("#list_dau_noi_tb_find_khoa_sat").combotree({
    //        url: _nameSubDomain + "/ONTGpon/getlistDauNoiTbBySiteid?siteId=" + id,
    //        valueField: 'THIETBIID',
    //        textField: 'TEN',
    //        onSelect: function (row) {

    //            //alert(row.id);
    //            $('#List_TBGPON_VIEW_KS_ON_MAPS').jtable('load', { TbId: row.id });

    //        },
    //        onLoadSuccess: function (row) {
    //            // $("#list_don_vi_phan_quyen_thong_bao").combotree('setValue', 1);
    //        }
    //    });
    //}, 500);

    $('.List_TBGPON_VIEW_KS_ON_MAPS_').jtable({

        title: 'DS LẮP THUÊ BAO GPON',
        openChildAsAccordion: true,
        actions: {
            listAction: _nameSubDomain + '/ONTGpon/ListONTHUEBAOByTbArrayId?TbId=' + $("#id_first_dau_noi_tb_find_khoa_sat").val(),

        },

        fields: {
            ONTID: {
                key: true,
                edit: false,
                create: false,
                //list:false,
                width: '3%',
                display: function (tbData) {
                    var $img_port = $('<img src="' + _nameSubDomain + '/Content/images/congviec-icon.png" style="cursor:pointer;" title="Thuê Bao GPON" />');
                    //Open child table when user clicks the image
                    $img_port.click(function () {
                        $('.List_TBGPON_VIEW_KS_ON_MAPS_').jtable('openChildTable',
                               $img_port.closest('tr'), //Parent row
                               {
                                   title: 'DS THUÊ BAO GPON ',
                                   actions: {
                                       listAction: _nameSubDomain + '/ONTGpon/ListTHUEBAOONTByTbId?ontId=' + tbData.record.ONTID,
                                   },
                                   fields: {
                                       ID: {
                                           key: true,
                                           list: false,
                                       },
                                       THUEBAO_ID: {
                                           title: 'Thuê Bao',
                                           width: '30%',
                                           display: function (data) {
                                               if (data.record.THUEBAO_ID == null) return "";
                                               else return GetThueBaoCssOntById(data.record.THUEBAO_ID).TEN_TB;
                                           }
                                       },
                                       SITE_ID: {
                                           title: 'Vnpt Site',
                                           width: '30%',
                                           display: function (data) {
                                               if (data.record.SITE_ID == null) return "";
                                               else return Get_vnpt_site_ById(data.record.SITE_ID).TEN;
                                           }
                                       },
                                       GHI_CHU: {
                                           title: 'Ghi Chú',
                                           width: '20%'
                                       },

                                   }
                               }, function (data) { //opened handler
                                   data.childTable.jtable('load');
                               });
                    });
                    //Return image to show on the person row
                    return $img_port;
                }

            },
            THUTU: {
                title: 'Thứ tự',
                width: '20%'
            },
            ONU_ID: {
                title: 'ONT Id',
                width: '20%'
            },
            VISA: {
                title: 'Visa',
                width: '10%'
            },
            SERIAL: {
                title: 'Serial',
                width: '10%'

            },
            //ACCOUNT: {
            //    title: 'Account',
            //    width: '10%'
            //},
            THIETBI_ONT: {
                title: 'Thiết bị ONT',
                width: '20%'
            },
            TRANGTHAI: {
                title: 'Trạng thái',
                width: '20%',
                display: function (data) {
                    return getTrangthaibyId(data.record.TRANGTHAI).TRANGTHAI1;
                }
            },
            //GHI_CHU: {
            //    title: 'Ghi chú',
            //    width: '20%'
            //},

        },
        //Initialize validation logic when a form is created
        formCreated: function (event, data) {
            //data.form.find('input[name="TEN"]').addClass('validate[required]');
            data.form.validationEngine();
        },

    });

    $('.List_TBGPON_VIEW_KS_ON_MAPS_').jtable('load');

    Load_Gian_Do_Dau_Noi_On_Maps("giando_load__dau_noi_maps_khao_sat", id);

    $(".Maps_pageInDex_ks").unmask();

}

function Load_Gian_Do_Dau_Noi_On_Maps(selectDIV, siteId) {

    $("." + selectDIV).html("");

    d3.json(_nameSubDomain + "/ONTGpon/LoadJsonGianDoOnGPON_BySite?siteId=" + siteId, function (json) {


        var m = [10, 80, 10, 80],
             w = 600 - m[1] - m[3],
             h = 350,
             i = 0,
             root;

        if (json.length > 0) {




            var cluster = d3.layout.tree().size([h, w]);

            $("." + selectDIV).html("");

            var svg = d3.select("." + selectDIV).append("svg")
                .attr("width", w + m[1] + m[3])
                .attr("height", h)
                //.attr("width", 750)
                //.attr("height", 300)
                .append("g")
                .attr("transform", "translate(40,0)");

            var nodes = cluster.nodes(json[0]);

            var link = svg.selectAll("path.link")
                .data(cluster.links(nodes))
                .enter().append("path")
                .attr("class", "link")
                .attr("style", "cursor:pointer")
                .attr("d", elbow_maps);

            //svg.selectAll("path.link").each(function(d, i) {
            //    console.log(this, d, i);
            //});



            var node = svg.selectAll("g.node")
                .data(nodes)
                .enter().append("g")
                .attr("class", "node")
                .attr("transform", function (d) { return "translate(" + d.y + "," + d.x + ")"; });


            node.append("image")
                   .attr("class", function (d) {
                       return "node_class_gian_do node_imges" + d.id;
                   })
                   .attr("xlink:href", function (d) { return "" + _nameSubDomain + "/Content/fileUpload/" + d.Images + ""; })
                   //.on("click", function (d) { if (d.intArrayItemChild.toString() == "") { return ViewThueBaoOnOnt(d.id.toString(), d.id.toString(), d.name); } else return ViewThueBaoOnOnt(d.intArrayItemChild.toString(), d.id.toString(), d.name); })
                   .attr("width", "50px")
                   .attr("x", "-25px")
                   .attr("y", "-25px")
                   .attr("height", "50px");

            //node.append("circle")
            //    .attr("r", 15)
            //    .on("click", function(d){ makeDialog("Cập nhật site " + d.name,d.id, 350 , subdomain + "/Site/EditSite?SiteId=" + d.id); })
            //    .attr("class", function(d) { return "edit_odf"; })
            //    .attr("data-dialog-url", function(d) { return "/Site/EditSite?SiteId=" + d.id ; })
            //    .attr("data-dialog-title", function(d) { return d.name; })
            //    .attr("data-dialog-id", function(d) { return d.id; })
            //    .style("fill", function (d) { return d.Color; });

            node.append("text")
                .attr("dx", function (d) { return d.children ? 70 : -100; })
                .attr("dy", "2.8em")
                .attr("text-anchor", function (d) { return d.children ? "end" : "start"; })
                .style("fill", function (d) { return d.Color; })
                .text(function (d) { return d.name; });

           node.append("text")
                 .attr("dx", function (d) { return d.children ? 50 : -100; })
                 .attr("dy", "-2.8em")
                 .attr("text-anchor", function (d) { return d.children ? "end" : "start"; })
                 .style("fill", function (d) { return d.Color; })
                 .text(function (d) { return d.thongSoPort; });

            node.append("text")
              .attr("dx", function (d) { return d.children || d._children ? -150 : 10; })
              .attr("dy", "1.5em")
              .attr("style", "cursor:pointer")
              .attr("text-anchor", function (d) { return d.children ? "end" : "start"; })
              .attr("class", function (d) { return "name_line_path"; })
              //.on("click", function(d, i){ return makeDialog("Cập nhật cáp quang " + d.namepath,d.pathid, 350 , subdomain + "/CapQuang/EditCapQuang?CapId=" + d.pathid); })
              .text(function (d) { return d.PortSo; }); //d.namepath; });

        }
   });

}

function elbow_maps(d, i) {
    return "M" + d.source.y + "," + d.source.x
        + "V" + d.target.x + "H" + d.target.y;
}


var drawingpolygonmainmap_ks;
var _newShape_polyline_main_map_ks;
var drawingManager_line_ks;
var _newShape_find_Customer_show = new google.maps.InfoWindow();
var _array_capquang_for_mainmap_ks = [];

function cal_chieu_dai() {

    try {
        _newShape_find_Customer_show.close();
        _newShape_polyline_main_map_ks.setMap(null);
    }
    catch (exception) { }
    


    drawingManager_line_ks = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.POLYLINE,
        drawingControl: false,
        polylineOptions: {
            editable: true,
            fillColor: '#1c56a3',
            strokeColor: '#1c56a3',
            strokeWeight: 5
        },
        map: _mapsMainKS
    });
   

    

    google.maps.event.addListener(drawingManager_line_ks, 'overlaycomplete', function (e) {

        if (e.type == google.maps.drawing.OverlayType.POLYLINE) {

            try {
                _newShape_find_Customer_show.close();
            }
            catch (exception) { }

            drawingManager_line_ks.setDrawingMode(null);

            _newShape_polyline_main_map_ks = e.overlay;
            _newShape_polyline_main_map_ks.type = e.type;

            _array_capquang_for_mainmap_ks.push(_newShape_polyline_main_map_ks);

            var _line_vertices_path_main_map_ = _newShape_polyline_main_map_ks.getPath();
            var _get_all_latlon_line_ks;
            _get_all_latlon_line_ks = _line_vertices_path_main_map_.getArray();

            var stringDistance = totalDistancesDarwPath(_get_all_latlon_line_ks);

            var _contentstring_capquang_mainmap = '<div style="text-align:center; overflow: hidden;width:80px;height: 20px;">Tổng: <span style="color:red">' + stringDistance + '<span></div>';


            var _line_latLng_main_map_ = new google.maps.LatLng(_get_all_latlon_line_ks[_get_all_latlon_line_ks.length - 1].lat(), _get_all_latlon_line_ks[_get_all_latlon_line_ks.length - 1].lng());

            _newShape_find_Customer_show.setContent(_contentstring_capquang_mainmap);
            _newShape_find_Customer_show.open(_mapsMainKS);
            _newShape_find_Customer_show.setPosition(_line_latLng_main_map_);

            google.maps.event.addListener(_newShape_polyline_main_map_ks, 'rightclick', function (e) {
                if (_newShape_polyline_main_map_ks.type === google.maps.drawing.OverlayType.POLYLINE) {
                    try {
                        _newShape_find_Customer_show.close();
                    }
                    catch (exception) { }
                    _newShape_polyline_main_map_ks.setMap(null);
                }
            });

            google.maps.event.addListener(_line_vertices_path_main_map_, 'set_at', function () {
                //polygon object: yourPolygon
                try {
                    _newShape_find_Customer_show.close();
                }
                catch (exception) { }

                var _get_all_latlon_line_ks_chage;
                _get_all_latlon_line_ks_chage = _line_vertices_path_main_map_.getArray();

                var stringDistance_change = totalDistancesDarwPath(_get_all_latlon_line_ks_chage);

                var _contentstring_capquang_mainmap_change = '<div style="text-align:center; overflow: hidden;width:80px;height: 20px;">Tổng: <span style="color:red">' + stringDistance_change + '<span></div>';


                var _line_latLng_main_map_change = new google.maps.LatLng(_get_all_latlon_line_ks_chage[_get_all_latlon_line_ks_chage.length - 1].lat(), _get_all_latlon_line_ks_chage[_get_all_latlon_line_ks_chage.length - 1].lng());

                _newShape_find_Customer_show.setContent(_contentstring_capquang_mainmap_change);
                _newShape_find_Customer_show.open(_mapsMainKS);
                _newShape_find_Customer_show.setPosition(_line_latLng_main_map_change);

            });

            google.maps.event.addListener(_line_vertices_path_main_map_, 'insert_at', function () {
                //polygon object: yourPolygon
                try {
                    _newShape_find_Customer_show.close();
                }
                catch (exception) { }

                var _get_all_latlon_line_ks_insert_at;
                _get_all_latlon_line_ks_insert_at = _line_vertices_path_main_map_.getArray();

                var stringDistanceinsert_at = totalDistancesDarwPath(_get_all_latlon_line_ks_insert_at);

                var _contentstring_capquang_mainmapinsert_at = '<div style="text-align:center; overflow: hidden;width:80px;height: 20px;">Tổng: <span style="color:red">' + stringDistanceinsert_at + '<span></div>';


                var _line_latLng_main_map_insert_at = new google.maps.LatLng(_get_all_latlon_line_ks_insert_at[_get_all_latlon_line_ks_insert_at.length - 1].lat(), _get_all_latlon_line_ks_insert_at[_get_all_latlon_line_ks_insert_at.length - 1].lng());

                _newShape_find_Customer_show.setContent(_contentstring_capquang_mainmapinsert_at);
                _newShape_find_Customer_show.open(_mapsMainKS);
                _newShape_find_Customer_show.setPosition(_line_latLng_main_map_insert_at);

            });

            google.maps.event.addListener(_line_vertices_path_main_map_, 'remove_at', function () {
                //polygon object: yourPolygon
                try {
                    _newShape_find_Customer_show.close();
                }
                catch (exception) { }

                var _get_all_latlon_line_ksremove_at;
                _get_all_latlon_line_ksremove_at = _line_vertices_path_main_map_.getArray();

                var stringDistanceremove_at = totalDistancesDarwPath(_get_all_latlon_line_ksremove_at);

                var _contentstring_capquang_mainmap_remove_at = '<div style="text-align:center; overflow: hidden;width:80px;height: 20px;">Tổng: <span style="color:red">' + stringDistanceremove_at + '<span></div>';


                var _line_latLng_main_map_remove_at = new google.maps.LatLng(_get_all_latlon_line_ksremove_at[_get_all_latlon_line_ksremove_at.length - 1].lat(), _get_all_latlon_line_ksremove_at[_get_all_latlon_line_ksremove_at.length - 1].lng());

                _newShape_find_Customer_show.setContent(_contentstring_capquang_mainmap_remove_at);
                _newShape_find_Customer_show.open(_mapsMainKS);
                _newShape_find_Customer_show.setPosition(_line_latLng_main_map_remove_at);

            });

            google.maps.event.addListener(_newShape_polyline_main_map_ks, 'dblclick', function () {
                //polygon object: yourPolygon
                try {
                    _newShape_find_Customer_show.close();
                }
                catch (exception) { }

                var _line_vertices_path_main_map_click = _newShape_polyline_main_map_ks.getPath();

                var _get_all_latlon_line_ks_click;
                _get_all_latlon_line_ks_click = _line_vertices_path_main_map_click.getArray();

                var stringDistance_click = totalDistancesDarwPath(_get_all_latlon_line_ks_click);

                var _contentstring_capquang_mainmap_click = '<div style="text-align:center; overflow: hidden;width:80px;height: 20px;">Tổng: <span style="color:red">' + stringDistance_click + '<span></div>';


                var _line_latLng_main_map_click = new google.maps.LatLng(_get_all_latlon_line_ks_click[_get_all_latlon_line_ks_click.length - 1].lat(), _get_all_latlon_line_ks_click[_get_all_latlon_line_ks_click.length - 1].lng());

                _newShape_find_Customer_show.setContent(_contentstring_capquang_mainmap_click);
                _newShape_find_Customer_show.open(_mapsMainKS);
                _newShape_find_Customer_show.setPosition(_line_latLng_main_map_click);

            });

            

        }

    });



}

var market_vnpt_site_add_Drawing_findcustomer;
var market_vnpt_site_add_Drawing_Array__findcustomer = [];
var _new_vnpt_site_main_maps__findcustomer;


var _info_find_Customer_show = new google.maps.InfoWindow();

function find_point_customer_ks() {

    try {
        market_vnpt_site_add_Drawing_findcustomer.setDrawingMode(null);
        _new_vnpt_site_main_maps__findcustomer.setMap(null);
        _info_find_Customer_show.close();
    }
    catch (e) { }

    var option_customer_ks = {
        icon: _nameSubDomain + '/Content/images/ftth.png',
        editable: false,
        draggable: true
    };

    market_vnpt_site_add_Drawing_findcustomer = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.MARKER,
        drawingControl: false,
        markerOptions: option_customer_ks,
        map: _mapsMainKS
    });


    google.maps.event.addListener(market_vnpt_site_add_Drawing_findcustomer, 'overlaycomplete', function (e) {

        if (e.type == google.maps.drawing.OverlayType.MARKER) {

            market_vnpt_site_add_Drawing_findcustomer.setDrawingMode(null);

            _new_vnpt_site_main_maps__findcustomer = e.overlay;
            _new_vnpt_site_main_maps__findcustomer.type = e.type;

            market_vnpt_site_add_Drawing_Array__findcustomer.push(_new_vnpt_site_main_maps);

            var _marker_center_point_main_map_ks = _new_vnpt_site_main_maps__findcustomer.getPosition();

            var _contentstring_marker__khao_sat = '<div style="text-align:center; overflow: hidden;height:20px">Tọa độ: ' + '<b>lat:' + _marker_center_point_main_map_ks.lat() + ', long: ' + _marker_center_point_main_map_ks.lng() + '</b></div>';

            _info_find_Customer_show.setContent(_contentstring_marker__khao_sat);
            _info_find_Customer_show.open(_mapsMainKS);
            _info_find_Customer_show.setPosition(_new_vnpt_site_main_maps__findcustomer.getPosition());

            google.maps.event.addListener(_new_vnpt_site_main_maps__findcustomer, 'dblclick', function (event) {
                /////////////set thong tin moi
                ////////////////// show ra

                var _contentstring_marker__khao_sat_dbclick = '<div style="text-align:center; overflow: hidden;height:20px">Tọa độ: ' + '<b>lat:' + event.latLng.lat() + ', long: ' + event.latLng.lng() + '</b></div>';

                _info_find_Customer_show.close(_mapsMainKS);
                _info_find_Customer_show.setContent(_contentstring_marker__khao_sat_dbclick);
                _info_find_Customer_show.open(_mapsMainKS);
                _info_find_Customer_show.setPosition(event.latLng);

            });

            google.maps.event.addListener(_new_vnpt_site_main_maps__findcustomer, 'dragend', function (event) {
                /////////////set thong tin moi
                ////////////////// show ra
                var _contentstring_marker__khao_sat_dragend = '<div style="text-align:center; overflow: hidden;height:20px">Tọa độ: ' + '<b>lat:' + event.latLng.lat() + ', long: ' + event.latLng.lng() + '</b></div>';
                _info_find_Customer_show.close(_mapsMainKS);
                _info_find_Customer_show.setContent(_contentstring_marker__khao_sat_dragend);
                _info_find_Customer_show.open(_mapsMainKS);
                _info_find_Customer_show.setPosition(event.latLng);

            });

            google.maps.event.addListener(_new_vnpt_site_main_maps__findcustomer, 'rightclick', function (event) {

                try {
                    market_vnpt_site_add_Drawing_findcustomer.setDrawingMode(null);
                    _new_vnpt_site_main_maps__findcustomer.setMap(null);
                }
                catch (e) { }

            });
        }
    });

}

var pathLenghtDistance = [];

function totalDistancesDarwPath(markersPath) {

    var polylineLength = 0;
    var StringReturn = "";

    pathLenghtDistance = [];

    for (var i = 0; i < markersPath.length; i++) {

        var lat = parseFloat(markersPath[i].lat());
        var lng = parseFloat(markersPath[i].lng());
        var pointPath = new google.maps.LatLng(lat,lng);
        pathLenghtDistance.push(pointPath);
        if (i > 0)
            polylineLength += Math.round(google.maps.geometry.spherical.computeDistanceBetween(pathLenghtDistance[i], pathLenghtDistance[i - 1]));

    }

    if (polylineLength.toString().length >= 4) {

        StringReturn = (polylineLength / 1000).toFixed(1) + " Km";

    }
    else {
        StringReturn = polylineLength + " m";
    }

    return StringReturn;
    

}

var tooltip = function () {
    var id = 'tt';
    var top = 3;
    var left = 3;
    var maxw = 300;
    var speed = 10;
    var timer = 20;
    var endalpha = 95;
    var alpha = 0;
    var tt, t, c, b, h;
    var ie = document.all ? true : false;
    return {
        show: function (v, w) {
            if (tt == null) {
                tt = document.createElement('div');
                tt.setAttribute('id', id);
                t = document.createElement('div');
                t.setAttribute('id', id + 'top');
                c = document.createElement('div');
                c.setAttribute('id', id + 'cont');
                b = document.createElement('div');
                b.setAttribute('id', id + 'bot');
                tt.appendChild(t);
                tt.appendChild(c);
                tt.appendChild(b);
                document.body.appendChild(tt);
                tt.style.opacity = 0;
                tt.style.filter = 'alpha(opacity=0)';
                document.onmousemove = this.pos;
            }
            tt.style.display = 'block';
            c.innerHTML = v;
            tt.style.width = w ? w + 'px' : 'auto';
            if (!w && ie) {
                t.style.display = 'none';
                b.style.display = 'none';
                tt.style.width = tt.offsetWidth;
                t.style.display = 'block';
                b.style.display = 'block';
            }
            if (tt.offsetWidth > maxw) { tt.style.width = maxw + 'px' }
            h = parseInt(tt.offsetHeight) + top;
            clearInterval(tt.timer);
            tt.timer = setInterval(function () { tooltip.fade(1) }, timer);
        },
        pos: function (e) {
            var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
            var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
            tt.style.top = (u - h) + 'px';
            tt.style.left = (l + left) + 'px';
        },
        fade: function (d) {
            var a = alpha;
            if ((a != endalpha && d == 1) || (a != 0 && d == -1)) {
                var i = speed;
                if (endalpha - a < speed && d == 1) {
                    i = endalpha - a;
                } else if (alpha < speed && d == -1) {
                    i = a;
                }
                alpha = a + (i * d);
                tt.style.opacity = alpha * .01;
                tt.style.filter = 'alpha(opacity=' + alpha + ')';
            } else {
                clearInterval(tt.timer);
                if (d == -1) { tt.style.display = 'none' }
            }
        },
        hide: function () {
            clearInterval(tt.timer);
            tt.timer = setInterval(function () { tooltip.fade(-1) }, timer);
        }
    };
}();

function print_maps_khao_sat() {

    var content = window.document.getElementById("print_map_canvas_ks"); // get you map details
    var newWindow = window.open(); // open a new window
    newWindow.document.write(content.innerHTML); // write the map into the new window
    newWindow.print(); // print the new window

}

function screenshot_maps_khao_sat() {

    html2canvas($('#print_map_canvas_ks'), {
        useCORS: true,
        onrendered: function (canvas) {
            var dataUrl = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
            //downloadImg(dataUrl, "filebando.jpg", "image/jpeg");
            window.location.href = dataUrl;
        }
    });



}

